//
//  SEGFocusViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGFocusViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>

@implementation SEGFocusViewModel

-(instancetype)init{
    if (self == [super init]) {
        _contentTitle = @"";
        _image = @"";
        _widgetWidth = LYScreenWidth();
        
    }
    return self;
}
@end

/**************************SEGFocusViewLayout************************/
@implementation SEGFocusViewLayout
- (instancetype)initWithData:(SEGFocusViewModel *)data{
    if (self == [super init]) {
        _data = data;
    }
    [self segFocusViewLayout];
    return self;
}

-(void)segFocusViewLayout{
    
    //高度不固定
    //_totalHeight = 0;
    _contentTitleLabelHeight = 0;
    _contentTitleLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
    
    if (!_data.channelName || _data.channelName.length == 0) {
        _totalHeight = bgViewHeight;
    }else {
        _totalHeight = bgViewHeight + 33;//背景bgViewHeight + 标签33
    }
    
}

//数据处理
-(void)layout{
    [self segcontentTitleLabelWithContent:_data.contentTitle fontSize:13 textColor:kColor_1];
    [self segChannelNameLabelWithContent:_data.channelName fontSize:12 textColor:kColor_S3];
}
//控件contentTitleLabel文本布局计算
-(void)segcontentTitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - bgViewHeight - 20, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 2;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd..."
    
    _contentTitleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentTitleLabelTextLayout)return;
    _contentTitleLabelHeight = [modifier heightForLineCount:_contentTitleLabelTextLayout.rowCount];
}

//控件contentTitleLabel文本布局计算
-(void)segChannelNameLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    if(content.length == 0) {
        return;
    }
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    
    
    NSString *channelName = [NSString stringWithFormat:@"%@主站",content];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:channelName];
    text.yy_font = font;
    text.yy_color = color;
    
    //图片
    CGFloat padding = 3;
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_zhuzhan")];
    NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:image FontSize:image.size.height contentInsets:UIEdgeInsetsMake(0, 10, 0, padding)];
    if (img) {
        [text insertAttributedString:img atIndex:0];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - 20, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    //    container.truncationType = SEGTextTruncationTypeEnd;// "abcd..."
    
    _channelNameLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //    if (!_contentTitleLabelTextLayout)return;
    //    _channelNameLabelHeight = [modifier heightForLineCount:_contentTitleLabelTextLayout.rowCount];
    CGFloat w = [SEGKitHelper seg_sizeWithString:channelName fontSize:fontSize textColor:color].width;
    CGFloat labelWidth = w + padding + image.size.width + 20;
    _channelNameLabelWidth = labelWidth < _data.widgetWidth ? labelWidth : _data.widgetWidth;
}

@end

